package com.zzld.modules.sys.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.zzld.core.utils.MessageUtils;
import com.zzld.core.utils.UuidUtil;
import com.zzld.modules.common.dto.CommonJSON;
import com.zzld.modules.common.dto.Grid;
import com.zzld.modules.common.dto.TreeGrid;
import com.zzld.modules.sys.entity.Role;
import com.zzld.modules.sys.security.ShiroUser;
import com.zzld.modules.sys.service.IResService;
import com.zzld.modules.sys.service.IRoleResService;
import com.zzld.modules.sys.service.IRoleService;
import com.zzld.modules.sys.service.IUserRoleService;

@Controller
@RequestMapping(value = "/sys/role")
public class RoleController {

	@Autowired
	private IRoleService roleService;

	@Autowired
	private IResService resService;

	@Autowired
	private IRoleResService roleResService;
	
	@Autowired
	private IUserRoleService userroleService;

	private String listurl = "/modules/sys/role/roleList";
	private String editurl = "/modules/sys/role/roleCru";
	private String ganturl = "/modules/sys/role/roleGrant";

	@RequestMapping(method = RequestMethod.GET)
	public String res(Model model) {
		return listurl;
	}

	// 进入授权页面
	@RequestMapping(value = "/grant/tree/pro")
	public String view(HttpServletRequest request, Model model) {
		model.addAttribute("id", request.getParameter("id"));
		return ganturl;
	}

	// 执行修改角色操作
	@RequestMapping(value = "/grant/tree/adup", method = RequestMethod.POST)
	@ResponseBody
	public CommonJSON adipe(HttpServletRequest request) {
		String role_id = request.getParameter("roleid");
		String ids = request.getParameter("ids");
		CommonJSON paramJson = new CommonJSON();
		if (roleResService.updateRoleRes(role_id, ids) > 0) {
			paramJson.setMessage(MessageUtils.EDIT_SUCCESS);
			paramJson.setSuccess(true);
		} else {
			paramJson.setMessage(MessageUtils.EDIT_WARING);
			paramJson.setSuccess(false);
		}
		return paramJson;
	}

	// 进入修改页面
	@RequestMapping(value = "/update/pre")
	public String update(Model model, HttpServletRequest request)
			throws Exception {
		return editurl;
	}

	/**
	 * 根据ID获取数据
	 * 
	 * @param request
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/selectById")
	@ResponseBody
	public Role selectById(HttpServletRequest request,
			@RequestParam("id") String id) {
		Role entity = null;
		if (id != null && !id.equals("")) {
			try {
				return roleService.selectByPrimaryKey(id);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return entity;
	}

	/**
	 * 修改/添加操作
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public CommonJSON comJson(HttpServletRequest request, Model model, Role role) {
		CommonJSON json = new CommonJSON();
		ShiroUser userq = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		role.setCreateBy(userq.getName());
		if (StringUtils.isBlank(role.getId())) {// 新增
			role.setId(UuidUtil.uuid().toString());

			try {
				roleService.save(role);
				json.setSuccess(true);
				json.setMessage(MessageUtils.ADD_SUCCESS);
			} catch (Exception e) {
				e.printStackTrace();
				json.setSuccess(false);
				json.setMessage(MessageUtils.ADD_FAIL);
			}
		} else {
			try {
				roleService.updateByPrimaryKeySelective(role);
				json.setSuccess(true);
				json.setMessage(MessageUtils.EDIT_SUCCESS);
			} catch (Exception e) {
				e.printStackTrace();
				json.setSuccess(false);
				json.setMessage(MessageUtils.EDIT_WARING);
			}
		}
		return json;
	}

	/**
	 * 删除操作
	 * 
	 * @throws Exception
	 */
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
	@ResponseBody
	public CommonJSON delete(HttpServletRequest request,
			@PathVariable("id") String id) {
		CommonJSON resultJson = new CommonJSON();

		// 执行删除
		try {

			if (roleService.deleteByPrimaryKey(id) > 0) {
				userroleService.selectbyroleid(id);
				resultJson.setMessage(MessageUtils.DELETE_SUCCESS);
			} else {
				resultJson.setMessage(MessageUtils.EDIT_WARING);
			}

		} catch (Exception e) {
			e.printStackTrace();
			resultJson.setMessage(MessageUtils.EDIT_WARING);
		}
		return resultJson;
	}

	@RequestMapping(value = "list")
	@ResponseBody
	public Grid list(HttpServletRequest request) {

		String name = request.getParameter("QUERY_t#name_S_LK") == null ? ""
				: request.getParameter("QUERY_t#name_S_LK");
		String pageNum = request.getParameter("page");
		String pageSize = request.getParameter("rows");
		String sort = request.getParameter("sort") == "seq" ? "sort" : request
				.getParameter("sort");
		String order = request.getParameter("order") == null ? "asc" : request
				.getParameter("order");
		if (sort.equals("createDate")) {
			sort = "create_date";
		}

		Grid n = new Grid();
		Map map = new HashMap<Object, String>();
		map.put("sort", sort);
		map.put("order", order);
		map.put("name", name);
		PageInfo page = roleService.treegrid(map, Integer.parseInt(pageNum),
				Integer.parseInt(pageSize));

		n.setRows(page.getList());
		n.setTotal(page.getTotal());
		return n;

	}

	@RequestMapping(value = "/userlist/tree")
	@ResponseBody
	public List<TreeGrid> tree(HttpServletRequest request, Model model) {
		return roleService.treeGridall();
	}

	@RequestMapping(value = "/userlist/treebyid", method = RequestMethod.GET)
	@ResponseBody
	public List<TreeGrid> treebyid(HttpServletRequest request, Model model) {
		String id = request.getParameter("userid");
	
		return roleService.treeGridbyid(id);
	}

}
